#include "CommonInfoCollection.h"

CommonInfoCollection::CommonInfoCollection(QObject *parent)
    : InfoCollectionBase(parent)
    , mainBoardSerialNumberCmd("dmidecode -t baseboard 2>/dev/null|grep -i 'Serial Number:'|cut -f2 -d:")
    , systemVersionCmd("dmidecode -t system 2>/dev/null|grep 'Version'|cut -f2 -d:")
    , systemSerialNumberCmd("dmidecode -t system 2>/dev/null|grep 'Serial Number'|cut -f2 -d:")
    , systemUuidCmd("dmidecode -t system 2>/dev/null|grep 'UUID'|cut -f2 -d:")
    , kernelVersionCmd("uname -r 2>/dev/null")
    , processorModelCmd("lscpu  2>/dev/null| grep 'Model name'|awk -F':' '{print $2}'")
    , graphicsCardModelCmd("lshw -c display  2>/dev/null|grep 'product'|awk -F':' '{print $2}'")
    , hardDiskSerialNumberCmd("lsblk -o SERIAL -d 2>/dev/null|sed -r '/^\s*$/d'|tail -n +2")
    , systemManufacturerCmd("dmidecode -t system 2>/dev/null|grep 'Manufacturer:'|cut -f2 -d:")
    , systemProductNameCmd("dmidecode -t system 2>/dev/null|grep 'Product Name'|cut -f2 -d:")
    , machineIdCmd("cat /etc/machine-id 2>/dev/null")
    , networkInfoCollection(new NetworkInfoCollection(parent))
{
}

void CommonInfoCollection::getInfo(CommonInfo &commonInfo)
{
    QList<QString> processModelList;
    QList<QString> graphicsCardModelList;
    QList<QString> hardDiskSerialNumberList;
    //  主板序列号
    getOnePopenResult(mainBoardSerialNumberCmd,commonInfo.mainBoardSerialNumber);
    //  系统版本
    getOnePopenResult(systemVersionCmd, commonInfo.systemVersion);
    //  系统序列号
    getOnePopenResult(systemSerialNumberCmd, commonInfo.systemSerialNumber);
    //  系统uuid
    getOnePopenResult(systemUuidCmd, commonInfo.systemUuid);
    //  内核版本
    getOnePopenResult(kernelVersionCmd, commonInfo.kernelVersion);
    //  处理器类型
    getPopenResult(processorModelCmd, processModelList);
    commonInfo.processorModel   =   processModelList.join(s_separator);
    //  显卡类型
    getPopenResult(graphicsCardModelCmd, graphicsCardModelList);
    commonInfo.graphicsCardModel = graphicsCardModelList.join(s_separator);
    //  硬盘类型
    // getHardDiskSerialNumber(commonInfo.hardDiskSerialNumber);
    getPopenResult(hardDiskSerialNumberCmd, hardDiskSerialNumberList);
    commonInfo.hardDiskSerialNumber = hardDiskSerialNumberList.join(s_separator);
    //  网卡mac
    commonInfo.networkMacAddress = networkInfoCollection->getNetworkMac();
    //  网卡类型
    commonInfo.networkType = networkInfoCollection->getNetworkType();
    //  厂商名
    getOnePopenResult(systemManufacturerCmd,commonInfo.systemManufacturer);
    //  产品名
    getOnePopenResult(systemProductNameCmd,commonInfo.systemProductName);
    //  获取操作系统机器id
    getOnePopenResult(machineIdCmd, commonInfo.machineId);
}

void CommonInfoCollection::getHardDiskSerialNumber(QString &serialNumber)
{
    QList<QString> mountNameResultList;
    QList<QString> serialNumberList;
    getPopenResult(hardDiskSerialNumberCmd, mountNameResultList);
    for (auto iter = mountNameResultList.begin();iter != mountNameResultList.end();iter++) {
        QString hardDiskName(iter->simplified());
        QString filePath = QString("/sys/block/%1/device/serial").arg(hardDiskName);
        QFile serialFile(filePath);
        if (!serialFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qDebug() << "serialfile open failed!" << filePath;
        } else {
            serialNumberList<<serialFile.readAll().simplified();
            serialFile.close();
        }
    }
    serialNumber = serialNumberList.join(s_separator);
}

